# -*- coding: utf-8 -*-



import  wx
import  os
import sys
from time import *
from time import time, strftime, localtime

from gettext import gettext as _

from features.UI.perspective import  baseview

from features.UI.common import images
from features.utils import dirs

class MyTreeCtrl(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)


    def OnCompareItems(self, item1, item2):
        t1 = self.GetItemText(item1)
        t2 = self.GetItemText(item2)
        if t1 < t2: return -1
        if t1 == t2: return 0
        return 1

class Template(baseview.BaseView):
    def __init__(self,parent):
        baseview.BaseView.__init__(self, parent)
        """
        mt = wx.TextCtrl(self, -1,
             u"Template\n",
             style=wx.TE_MULTILINE)
        #self.wd.set("LOG",mt.GetId())
        
        #print "Log:",mt.GetId()
        mt.SetInsertionPoint(1)         
        """
        
        isz = (16,16)
        il = wx.ImageList(isz[0], isz[1])
        fldridx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, isz))
        fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN,   wx.ART_OTHER, isz))
        fileidx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
        smileidx    = il.Add(images.getSmilesBitmap())

        
        self.tree = MyTreeCtrl(self, -1,wx.DefaultPosition, wx.DefaultSize, wx.TR_HAS_BUTTONS)
        self.root = self.tree.AddRoot(u"Template")
        self.tree.SetPyData(self.root, None)
        
        self.tree.SetImageList(il)
        self.il = il
        
        self.tree.SetItemImage(self.root, fldridx, wx.TreeItemIcon_Normal)
        self.tree.SetItemImage(self.root, fldropenidx, wx.TreeItemIcon_Expanded)        
        
        odir = dirs.Dirs('project.cfg')
        dirlist = odir.getSublist()
        
        #result_set = ['test','com','tem']
        for x in  dirlist:# result_set:
            #print x['name']
            child = self.tree.AppendItem(self.root, x['name'])  # data from database need to be docode
            self.tree.SetPyData(child, x)
            if x['isdir']:
                self.tree.SetItemImage(child, fldridx, wx.TreeItemIcon_Normal)
                self.tree.SetItemImage(child, fldropenidx, wx.TreeItemIcon_Expanded)
            else: #fileidx
                self.tree.SetItemImage(child, fileidx, wx.TreeItemIcon_Normal)
            if x['isdir']:
                subdirlist = odir.getSublist(x['name'])
                for y in subdirlist:
                    last = self.tree.AppendItem(child, y['name'])
                    self.tree.SetPyData(last, None)
                    if y['isdir']:
                        self.tree.SetItemImage(last, fldridx, wx.TreeItemIcon_Normal)
                        self.tree.SetItemImage(last, fldropenidx, wx.TreeItemIcon_Expanded)            
                    else:
                        self.tree.SetItemImage(last, fileidx, wx.TreeItemIcon_Normal)
                        
            #self.tree.Expand(child)    
        self.tree.Expand(self.root)
        
        #self.BuildRight()
        #self.p2.ctrl = wx.TextCtrl(self.p2, -1, "xxx")
        
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate, self.tree)
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
        
    def OnActivate(self,event):
        #print "OnActivate"
        id = self.getDict("HELP")
        if id:
            textctrl = wx.FindWindowById(id)
            now = strftime("%Y-%m-%d %H:%M:%S", localtime())
            text = u"%s: %s" %(now, "value")
            textctrl.AppendText(text+"\n")         
        
        event.Skip()

    def OnSelChanged(self,event):
        id = self.getDict("HELP")
        if id:
            textctrl = wx.FindWindowById(id)
            now = strftime("%Y-%m-%d %H:%M:%S", localtime())
            
            self.item = event.GetItem()
            count = self.tree.GetChildrenCount(self.item)
            value =  self.tree.GetItemText(self.item)
            #itemdata = self.tree.GetItemData(self.item)
            #print itemdata.GetId()
            #obj = self.tree.GetItemPyData(self.item)
            map_set = self.tree.GetPyData(self.item)            
            text = u"%s: %s" %(now, value)
            textctrl.AppendText(text+"\n")                
        event.Skip()

        